//
//  LWProductCell.m
//  1.28  weibo
//
//  Created by mac on 15/3/1.
//  Copyright (c) 2015年 mac. All rights reserved.
//

#import "LWProductCell.h"
#import "LWProDuctItme.h"
#import "LWProductArrowItme.h"
#import "LWproductItmeSwitchItme.h"
#import "LWproductLableItme.h"
#import "LWBadgeBtn.h"
#import "LWProductchectItme.h"
@interface LWProductCell ()

@property (nonatomic,strong) UIImageView *arrowView;
@property (nonatomic,strong) UISwitch *switchBtn;
@property (nonatomic,strong) UILabel *lableView;
@property (nonatomic,strong) LWBadgeBtn *badgeBtn;
@property (nonatomic,strong) UIButton *checkBtn;
@end

@implementation LWProductCell

+ (instancetype)cellWithTableView:(UITableView *)tableView{

    static NSString *ID = @"settingCell";
    LWProductCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[LWProductCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
    }
    return cell;
}

- (void)setItme:(LWProDuctItme *)itme{

    _itme = itme;
    
    //1.设置标题
    self.textLabel.text = itme.title;
    //2.设置副标题
    self.detailTextLabel.text = itme.subTitle;
    //3.设置图片
    if (itme.icon) {
        self.imageView.image = [UIImage imageNamed:itme.icon];
    }
    //4.设置辅助视图
    if (_itme.badgeValue != nil) {
        self.badgeBtn.badgeValue = _itme.badgeValue;
        self.accessoryView = self.badgeBtn;
    }else if ([_itme isKindOfClass:[LWProductArrowItme class]]) {
        self.accessoryView = self.arrowView;
    }else if ([_itme isKindOfClass:[LWproductItmeSwitchItme class]]){
    
        self.accessoryView = self.switchBtn;
        //设置cell的选中样式
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        NSUserDefaults *defause = [NSUserDefaults standardUserDefaults];
        self.switchBtn.on = [defause boolForKey:self.itme.title];
    }else if ([_itme isKindOfClass:[LWproductLableItme class]]){
    
        LWproductLableItme *itme = (LWproductLableItme *)_itme;
        self.lableView.text = itme.text;
        [self.lableView sizeToFit];
        self.accessoryView = self.lableView;
    }else if ([_itme isKindOfClass:[LWProductchectItme class]]){
        
        
        LWProductchectItme *itme = (LWProductchectItme *)_itme;
        
        self.accessoryView = self.checkBtn;
        if (itme.check) {
            self.checkBtn.selected = YES;
            
        }else{
            self.checkBtn.selected = NO;
        }
        

        
//        NSUserDefaults *defause = [NSUserDefaults standardUserDefaults];
//        self.checkBtn.selected = [defause boolForKey:self.itme.title];

    }else{
    
        self.accessoryView = nil;
    }
}


#pragma mark - 懒加载
- (UILabel *)lableView{

    if (_lableView == nil) {
        _lableView = [[UILabel alloc] init];
    }
    return _lableView;
}

- (UIImageView *)arrowView{

    if (_arrowView == nil) {
        _arrowView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CellArrow"]];
    }
    return _arrowView;
}

- (UISwitch *)switchBtn{

    if (_switchBtn == nil) {
        _switchBtn = [[UISwitch alloc] init];
        [_switchBtn addTarget:self action:@selector(switchBtnClick) forControlEvents:UIControlEventValueChanged];
    }
    return _switchBtn;
}
- (void)switchBtnClick{

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    //以每个cell的标题进行存储
    [userDefaults setBool:self.switchBtn.isOn forKey:self.itme.title];
    [userDefaults synchronize];
}

- (LWBadgeBtn *)badgeBtn{

    if (_badgeBtn == nil) {
        _badgeBtn = [[LWBadgeBtn alloc] init];
    }
    return _badgeBtn;
}

- (void)layoutSubviews{

    [super layoutSubviews];
    self.detailTextLabel.x = CGRectGetMaxX(self.textLabel.frame) + 8;
}
-(UIButton *)checkBtn{

    if (_checkBtn == nil) {
        _checkBtn = [[UIButton alloc] init];
        [_checkBtn setBackgroundImage:[UIImage imageNamed:@"common_icon_arrow"] forState:UIControlStateNormal];
        [_checkBtn setBackgroundImage:[UIImage imageNamed:@"common_icon_checkmark"] forState:UIControlStateSelected];
        _checkBtn.size = CGSizeMake(44, 44);
       
    }
    return _checkBtn;
}
- (void)checkBtnClick:(UIButton *)btn{

        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    // 记录开关按钮, 以每一个开关对应的标题作为key，进行存储
    [defaults setBool:self.checkBtn.isSelected forKey:self.itme.title];
    // 立马同步
    [defaults synchronize];
}

@end
